$(function(){

	myselfChoose(1);
	
	$("#connectMember").click(function(){
		var mobile = $("#connectMobile").val().replace(/(^\s*)|(\s*$)/g, '')+"";
		$.post("connectMember",{"mobile":mobile,"cid":$("#nowCid").val()},function (data) {
			if(data==1){
                $("#info").css("display","block");
                $("#msg").html("<p style='font-size:30px;padding-top:30px;color:green;'>等待学员确认</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },1000);
			}else if(data==2){
                $("#info").css("display","block");
                $("#msg").html("<p style='font-size:25px;padding-top:30px;color:red;'>该学员不是会员</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },1500);
			}else if (data==3) {
                $("#info").css("display","block");
                $("#msg").html("<p style='font-size:30px;padding-top:30px;color:red;'>该用户不存在</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },1500);
			}else if (data==4){
                $("#info").css("display","block");
                $("#msg").html("<p style='font-size:30px;padding-top:30px;color:red;'>已有其他教练</p>");
                window.setTimeout(function(){
                    $("#info").css("display","none");
                },1500);
			}
        });

	});
});
//用于解析我的购买的课程
function addListToTable(data){
	        var teacher = $("#nowTeacher").val();//需获取当前教练的姓名
		    $("#myCourseTable tr:not(:eq(0))").remove();//在添加数据前,先删除标题以外的内容

			for(var i=0;i<data.length;i++){	
				//将records以js的形式添加到指定table中

				if(data[i].signState==0&&data[i].course.teacher==teacher&&data[i].numLast>0){
	    		$("#myCourseTable").append("<tr class='success'><td>"+
	    		        data[i].course.cname+"</td><td>"+
	    				data[i].course.teacher+"</td><td>"+
	    				data[i].classNum+"</td><td id='numSpan"+data[i].id+"'>"+
	    				data[i].numLast+"</td><td>"+
	    				"<button  id='signbtn"+data[i].id+"' style='background-color:#009F95' onclick='signInMyCourse("+data[i].id+")'>打卡</button>"+
	    				"</td></tr>");	
	    		}else if(data[i].signState==1&&data[i].course.teacher==teacher&&data[i].numLast>0){
	    			$("#myCourseTable").append("<tr class='info'><td>"+
	    		        data[i].course.cname+"</td><td>"+
	    				data[i].course.teacher+"</td><td>"+
	    				data[i].classNum+"</td><td>"+
	    				data[i].numLast+"</td><td>"+
	    				"<button  style='background-color: #9d9d9d' disabled='disabled'>已打</button>"+
	    				"</td></tr>");		    				
	    		}else if(data[i].course.teacher!=teacher&&data[i].numLast>0){
	    			$("#myCourseTable").append("<tr class='info'><td>"+
	    		        data[i].course.cname+"</td><td>"+
	    				data[i].course.teacher+"</td><td>"+
	    				data[i].classNum+"</td><td>"+
	    				data[i].numLast+"</td><td>"+
	    				"<button style='background-color: #9d9d9d' disabled='disabled'>无权</button>"+
	    				"</td></tr>");
	    		  }else if (data[i].numLast==0) {
                    $("#myCourseTable").append("<tr class='info'><td>"+
                        data[i].course.cname+"</td><td>"+
                        data[i].course.teacher+"</td><td>"+
                        data[i].classNum+"</td><td>"+
                        data[i].numLast+"</td><td>"+
                        "<button style='background-color:#1E9FFF' disabled='disabled'>结束</button>"+
                        "</td></tr>");
				}

				}

			
			if(data.length==0){
			$("#myCourseTable").append("<tr class='danger'><p style='text-align: center'>无数据</p></tr>");
			}
			}
			
		
//通过uid找到该用户购买的所有课程，并实现json赋值
function getMemberCourses(uid){
	$.post("/getMyCourses",{"uid":uid},function (data) {
        addListToTable(data);
    });
}

//将该预约课程的ID提交，实现打卡效果
function signInMyCourse(id){

	$.post("/signInMyCourse",{"id":id},function (data) {
        $("#signbtn"+id).attr("disabled","disabled");
        $("#signbtn"+id).text("已打");
        $("#signbtn"+id).css("background-color","#9d9d9d");
		$("#numSpan"+id).text(data);
    })
}

function stopConnectByCoach(uid){
	if(confirm("您确定要取消与该学员的联系吗？")){
		$.post("/connectMemberStop",{"uid":uid},function() {
            $("#info").css("display","block");
            $("#msg").html("<p style='font-size:40px;padding-top:30px;color:green;'>成功</p>");
            window.setTimeout(function(){
                $("#info").css("display","none");
            },1500);
        });
        myselfChoose(1);
	}
}

//解析我的学员信息
function showMyMembers(data){
	var member ="";
	for(var i=0 ;i<data.length;i++){
        if(data[i].cidState==1){
		member = member + 
		  "<div class='col-sm-3 col-xs-12'>"+
             "<a href='' onclick='getMemberCourses("+data[i].uid+")' data-toggle='modal' data-target='#myModal'>"+
                "<div class='trainers-box'>"+
                    "<figure>"+
                    	"<img src='img/"+data[i].myself+"'>"+
                    "</figure>"+
                    "<span style='border-bottom: 1px #000000 solid;'>"+data[i].nickname+"</span>"+
                    "<span style='border-bottom: 1px #000000 solid;'>"+data[i].mobile+"</span>"+
            "<a href='' onclick='stopConnectByCoach("+data[i].uid+")'>"+"<span style='color: #0f0f0f;'>"+"取消该用户关联"+"</span>"+"</a>"+
                "</div>"+  
              "</a>"+
            "</div>";
		}else{
            member = member +
                "<div class='col-sm-3 col-xs-12'>"+
                "<div class='trainers-box'>"+
                "<figure>"+
                "<img src='img/"+data[i].myself+"'>"+
                "</figure>"+
                "<span style='border-bottom: 1px #000000 solid;'>"+"等待用户确认"+"</span>"+
                "<span style='border-bottom: 1px #000000 solid;'>"+data[i].mobile+"</span>"+
                "<a href='' onclick='stopConnectByCoach("+data[i].uid+")'>"+"<span style='color: #0f0f0f;'>"+"取消该用户关联"+"</span>"+"</a>"+
                "</div>"+
                "</div>";
		}
	}	
	$("#membersDiv").html(member);
	if(member==""){
        $("#membersDiv").html("<h1 style='text-align: center;'>您还未有学员</h1>");
	}
}


function myselfChoose(i){
	if(i==1){
		//改变导航栏样式
		$("#oneLi").addClass("active");
		$("#twoLi").removeClass("active");       			
		//改变所展示的课程信息的显示情况
		$("#oneContainer").css("display","block");
		$("#twoContainer").css("display","none");
        $.post("/getMemberByCid",{"cid":$("#nowCid").val()},function (data) {
        	console.log(data);
            showMyMembers(data);
        });

	}
	if(i==2){
		$("#oneLi").removeClass("active");
		$("#twoLi").addClass("active");       		
		$("#oneContainer").css("display","none");
		$("#twoContainer").css("display","block");
	}
}    


/**
 * 我的课程相关js 
 */
var data = [{
	  "id":1,
	  "classNum":20,
	  "numLast":18,
	  "date":"2020-3-20 18:23:29",
	  "signState":1,
	  "course":{
	  	"cname":"力量举卧推",
	  	"sample":"feature-thumb1.png",
	  	"teacher":"梁启超",
	  	"price":200
	  }
},
{"id":2,
	  "classNum":30,
	  "numLast":12,
	  "date":"2020-3-20 12:23:29",
	  "signState":0,
	  "course":{
	  	"cname":"力量举深蹲",
	  	"sample":"feature-thumb2.png",
	  	"teacher":"梁启超",
	  	"price":200
	  }
},
{"id":3,
	  "classNum":10,
	  "numLast":8,
	  "date":"2020-3-20 23:23:29",
	  "signState":1,
	  "course":{
	  	"cname":"力量举硬拉",
	  	"sample":"feature-thumb3.png",
	  	"teacher":"王可可",
	  	"price":200
	  }
}];

	var users = [{
	"uid":1,
	"username":"钱学森",
	"mobile":"13544412541",
	"myself":"trainer1.png"
},{
	"uid":2,
	"username":"毛泽东",
	"mobile":"13544412542",
	"myself":"trainer2.png"
},{
	"uid":3,
	"username":"周恩来",
	"mobile":"13544412543",
	"myself":"trainer3.png"
}];